﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;

namespace YiSha.Service.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-16 23:04
    /// 描 述：检测机械信息服务类
    /// </summary>
    public class DetectionMechanicsService : RepositoryFactory
    {
        #region 获取数据

        public async Task<List<DetectionMechanicsEntity>> GetList(DetectionMechanicsListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<DetectionMechanicsEntity>> GetPageList(DetectionMechanicsListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<DetectionMechanicsEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<DetectionMechanicsEntity>(id);
        }

        public async Task<DetectionMechanicsEntity> GetEntity(string code)
        {
            return await this.BaseRepository().FindEntity<DetectionMechanicsEntity>(t => t.HuanBaoCode == code);
        }

        #endregion 获取数据

        #region 提交数据

        public async Task SaveForm(DetectionMechanicsEntity entity)
        {
            await this.BaseRepository().BeginTrans();
            try
            {
                if (entity.Id.IsNullOrZero())
                {
                    await entity.Create();
                    await this.BaseRepository().Insert(entity);
                }
                else
                {
                    await entity.Modify();
                    await this.BaseRepository().Update(entity);
                    var mechanics = await this.BaseRepository().FindEntity<MechanicsEntity>(t => t.HuanBaoCode == entity.HuanBaoCode);
                    if (mechanics==null)
                    {
                        mechanics = new MechanicsEntity();
                    }
                    mechanics.CopyFromDetectionMechanics(entity);
                    await this.BaseRepository().Update(mechanics);
                }
                await this.BaseRepository().CommitTrans();
            }
            catch (Exception)
            {
                await this.BaseRepository().RollbackTrans();
                throw;
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<DetectionMechanicsEntity>(idArr);
        }

        #endregion 提交数据

        #region 私有方法

        private Expression<Func<DetectionMechanicsEntity, bool>> ListFilter(DetectionMechanicsListParam param)
        {
            var expression = LinqExtensions.True<DetectionMechanicsEntity>();
            if (param != null)
            {
                if (!param.Ids.IsNullOrEmpty())
                {
                    long[] idList = TextHelper.SplitToArray<long>(param.Ids, ',');
                    expression = expression.And(t => idList.Contains(t.Id.Value));
                }
            }
            return expression;
        }

        #endregion 私有方法
    }
}
